<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.util.ArrayList"%>
<%@page import="model.MonHocModel"%>
<%@page import="common.Constant" %>
<%@page import="dao.MonHocDAO"%>
<%@page import="dao.DangKyDAO"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Thông Tin Môn Học</title>
<link href="../style/css.css" rel="stylesheet" type="text/css" />
<link href="../script/check.js" language="JavaScript" type="text/javascript" />
<%String tenMH=""; %>
<%int indexPage=1; %>
<c:if test="${ not empty param.tenMH }">
	<% tenMH=request.getParameter("tenMH"); %>
</c:if>
<c:if test="${ not empty param.index}">
	<% indexPage=Integer.parseInt(request.getParameter("index")); %>
</c:if>
</head>
<c:set var='monHocList' value='<%= MonHocDAO.getAllMonHocByTen(indexPage,Constant.NUM_RECORD_SinhVien,tenMH)%>'></c:set>	
<c:set var='showNumPage' value='<%=MonHocDAO.showNumPage(indexPage,MonHocDAO.getTotalMonHocByTen(tenMH),"")%>'></c:set>
<body>
<div align="center">
	<!-- S HEAD CONTENT -->
	<jsp:include page="../header.jsp" />
<!-- E HEAD CONTENT -->
		<c:if test="${ not empty param.errUpdate}">
			<c:if test="${param.errUpdate eq 'false'}">
					<font color='blue' style='font-weight:bold'>Cập nhật thành công</font>
					<br/>
					<br/>
			</c:if>
			<c:if test="${param.errUpdate eq 'true'}">
					<font color='red' style='font-weight:bold'>Cập nhật thất bại</font>
					<br/>
					<br/>
			</c:if>
		</c:if>
		<c:if test="${ not empty param.errInsert}">
			<c:if test="${param.errInsert eq 'false'}">
					<font color='blue' style='font-weight:bold'>Tạo mới thành công</font>
					<br/>
					<br/>
			</c:if>
			<c:if test="${param.errInsert eq 'true'}">
					<font color='red' style='font-weight:bold'>Tạo mới thất bại</font>
					<br/>
					<br/>
			</c:if>
		</c:if>			
			
		<form name="frmMonHoc" id="frmMonHoc" method="post" action="<%=request.getContextPath()%>/thanhVienController" >
			<input type="hidden" name="actionType" id="actionType" value="monHoc" />
			<div>
				<table>
					<tr style="background-color: transparent;">
						<td colspan="9">
							<h3>Danh Sách Môn Học</h3>
						</td>
					</tr>
					<tr style="background-color: transparent;">
						<td colspan="9">
							<c:if test="${not empty param.xoa}">
								<b class="msg">Xoá môn học thành công !!!</b>
							</c:if>
						</td>
					</tr>
				</table>
					
				<table border="1" >	
					<tr>
						<td colspan="12">Tên môn học <input type="text" name="txtTimTenMonHoc" id="txtTimTenMonHoc"/>
							<a href = "javascript: click_btnTimTenMH()">
									<img src="<%=request.getContextPath()%>/images/timkiem.png" border = "0" />
								</a>
								&nbsp;&nbsp;
								<a href = "javascript: deleteRow()"><img src="<%=request.getContextPath()%>/images/xoa.png" border = "0" /></a>
								<a href = "danhsachmonhoc.jsp?stt=insert&tenMH=<%=tenMH %>&index=<%=indexPage %>">
									<img src="<%=request.getContextPath()%>/images/them.png" border = "0" />
								</a>
						</td>
					</tr>				
					<tr>
						<td colspan="12" style="text-align:right">	
							Trang 
							<c:forEach var="numPage" items="${showNumPage}">
								${numPage} 
							</c:forEach>									
						
						</td>
						
					</tr>
					<tr>
						<th bgcolor = "#186fb2">
								<div class = "div_textWhite">
									<input type="checkbox" name="chkAll" id="chkAll" onclick="checkAll();"></input>
								</div>
						</th>
						<th style='background-color: #186fb2;color:white' width="30px;">STT</th>
						<th style='background-color: #186fb2;color:white' width="45px;">MaMH</th>
						<th style='background-color: #186fb2;color:white' width="100px;">Tên môn học</th>
						<th style='background-color: #186fb2;color:white' width="90px;">TG bắt đầu</th>
						<th style='background-color: #186fb2;color:white' width="90px;">TG kết thúc</th>
						<th style='background-color: #186fb2;color:white' width="50px;">Chứng chỉ</th>
						<th style='background-color: #186fb2;color:white' width="50px;">Lệ phí</th>
						<th style='background-color: #186fb2;color:white' width="50px;">Tổng SV-ĐK</th>
						<th style='background-color: #186fb2;color:white' colspan="2"></th>
					</tr>
					<c:if test="${param.stt eq 'insert'}">
						<tr>
							<td colspan='12'>
								<br/>
								<br/>
								<font style='font-weight:bold'>Thêm mới môn học</font>
								<br/>
								<br/>
								<table class="tableMonHoc">
										<tr><td colspan="12"><font color='red' id="alertValidate"></font></td></tr>
										<tr>
											<td width="30%" style='font-weight:bold'>Mã môn học</td>
											<td width="70%">
												<input type='text' name="txtMaMonHoc" id="txtMaMonHoc" />
												<font color='red' id='alertMaMonHoc'></font>
											</td>
										</tr>
										<tr>
											<td style='font-weight:bold'>Tên môn học</td>
											<td width="70%">
												<input type='text' name="txtTenMonHoc" id="txtTenMonHoc" />
												<font color='red' id='alertTenMonHoc'></font>
											</td>
										</tr>
										<tr>
											<td style='font-weight:bold'>Thời gian bắt đầu</td>
											<td>
												<select name="selNgayBatDau" id="selNgayBatDau">
													<c:forEach var="NgayBatDau" begin="01" end="31">
														<option value="${NgayBatDau}">${NgayBatDau}</option>
													</c:forEach>
												</select>
												<font color='red' id='alertNgayBatDau'></font> /
												
												<select name="selThangBatDau" id="selThangBatDau">
													<c:forEach var="ThangBatDau" begin="01" end="12">
														<option value="${ThangBatDau}">${ThangBatDau}</option>
													</c:forEach>
												</select>/
												
												<select name="selNamBatDau" id="selNamBatDau">
													<c:forEach var="NamBatDau" begin="2011" end="2020">
														<option value="${NamBatDau}">${NamBatDau}</option>
													</c:forEach>
												</select>												
											</td>
										</tr>
										<tr>
											<td style='font-weight:bold'>Thời gian kết thúc</td>
											<td>
												<select name="selNgayKetThuc" id="selNgayKetThuc">
													<c:forEach var="NgayKetThuc" begin="01" end="31">
														<option value="${NgayKetThuc}">${NgayKetThuc}</option>
													</c:forEach>
												</select>
												<font color='red' id='alertNgayKetThuc'></font> /
												
												<select name="selThangKetThuc" id="selThangKetThuc">
													<c:forEach var="ThangKetThuc" begin="01" end="12">
														<option value="${ThangKetThuc}">${ThangKetThuc}</option>
													</c:forEach>
												</select>/
												
												<select name="selNamKetThuc" id="selNamKetThuc">
													<c:forEach var="NamKetThuc" begin="2011" end="2020">
														<option value="${NamKetThuc}">${NamKetThuc}</option>
													</c:forEach>
												</select>												
											</td>
										</tr>
										<tr>
											<td style='font-weight:bold'>Chứng chỉ</td>
											<td>
												<input type='text'  name="txtChungChi" id="txtChungChi"/>
												<font color='red' id='alertChungChi'></font>
											</td>
										</tr>
										<tr>
											<td style='font-weight:bold'>Lệ Phí</td>
											<td>
												<input type='text' name="txtLePhi" id="txtLePhi"/>
												<font color='red' id='alertLePhi'></font></td>
										</tr>
										
										<tr>	
											<td colspan="12" style="text-align:center">
											<a href = "javascript: click_btnThemMoi()">	
													<img src="<%=request.getContextPath()%>/images/chapnhan.png" border = "0" />
												</a>
												<a href = "javascript: click_btnThoat('<%=request.getParameter("index")%>')">	
													<img src="<%=request.getContextPath()%>/images/thoat.png" border = "0" />
												</a>
											</td>
										</tr>
									</table>
								
							</td>
						</tr>
					
					</c:if>
						<%int c = 1; 
						ArrayList<MonHocModel> monHocList;
						if(request.getSession().getAttribute("monHocModelList") != null)
							monHocList = (ArrayList<MonHocModel>)request.getSession().getAttribute("monHocModelList");
						else
						monHocList = MonHocDAO.getAllMonHoc();
					%>
					<c:forEach var="objMonHoc" items="${monHocList}">
							<c:choose>
								<c:when test="${param.stt eq 'view' and objMonHoc.maMH eq param.MaMH}">
									<tr><td  colspan="12">
										<table class="tableMonHoc">
											<tr>
												<td width="30%" style='font-weight:bold'>Mã môn học</td>
												<td width="70%">${objMonHoc.maMH }</td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Tên môn học</td>
												<td>${objMonHoc.tenMH }</td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Thời gian bắt đầu</td>
												<td>${objMonHoc.thoiGianBatDau }</td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Thời gian kết thúc</td>
												<td>${objMonHoc.thoiGianKetThuc }</td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Chứng chỉ</td>
												<td>${objMonHoc.chungChi }</td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Lệ phí</td>
												<td>${objMonHoc.lePhi }</td>
											</tr>											
										</table>		
									</td></tr>
								</c:when>
								<c:when test="${param.stt eq 'edit' and objMonHoc.maMH eq param.MaMH}">
									<input type='hidden' value='${objMonHoc.maMH}' name='txtMaMonHoc'></input>
									<tr><td colspan="12">
										<table class="tableMonHoc">
											<tr><td colspan="7"><font color='red' id="alertValidate"></font></td></tr>
											<tr>
												<td style='font-weight:bold'>Mã môn học</td>
												<td><input type='text' value="${objMonHoc.maMH }" readonly="readonly"/></td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Tên môn học</td>
												<td>
													<input type='text' name="txtTenMonHoc" id="txtTenMonHoc" value="${objMonHoc.tenMH }"/>
													<font color='red' id='alertTenMonHoc'></font></td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Thời gian bắt đầu</td>
												<td>
													<input type='text' name="txtThoiGianBatDau" id="txtThoiGianBatDau" value="${objMonHoc.thoiGianBatDau }"/>
													<font color='red' id='alertThoiGianBatDau'></font>(yyyy/mm/dd)</td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Thời gian kết thúc</td>
												<td>
													<input type='text' name="txtThoiGianKetThuc" id="txtThoiGianKetThuc" value="${objMonHoc.thoiGianKetThuc }"/>
													<font color='red' id='alertThoiGianKetThuc'></font>(yyyy/mm/dd)</td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Chứng chỉ</td>
												<td>
													<input type='text' value='${objMonHoc.chungChi}' name="txtChungChi" id="txtChungChi"/>
													<font color='red' id='alertChungChi'></font></td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Lệ phí</td>
												<td>
													<input type='text' value='${objMonHoc.lePhi}' name="txtLePhi" id="txtLePhi"/>
													<font color='red' id='alertLePhi'></font></td>
											</tr>
																		
											<tr>
												<td colspan="9" style="text-align:center">
												
													<a href = "javascript: click_btnCapNhat()">
														<img src="<%=request.getContextPath()%>/images/chapnhan.png" border = "0" />
													</a>
													<a href = "javascript: click_btnThoat('<%=request.getParameter("index")%>')">
														<img src="<%=request.getContextPath()%>/images/thoat.png" border = "0" />
													</a>											
												</td>												
											</tr>
										</table>
									
									</td></tr>
								</c:when>
								<c:otherwise>
									<c:set var="monHocList" value="<%= monHocList %>" scope = "session"></c:set>
									<tr>
											<td><input type="checkbox" name="chk<%=c %>" id="chk<%=c %>" onclick="checkRow();"/></td>
											<td><%=c %></td>
											<td>${objMonHoc.maMH }</td>
											<td><a href = "chitietmonhoc.jsp?maMonHoc=${objMonHoc.maMH }">${objMonHoc.tenMH }</a></td>
											<td>${objMonHoc.thoiGianBatDau }</td>
											<td>${objMonHoc.thoiGianKetThuc }</td>
											<td>${objMonHoc.chungChi }</td>
											<td>${objMonHoc.lePhi }</td>	
											<td>${objMonHoc.tongSVDK }</td>															
											<td><a href='danhsachmonhoc.jsp?stt=edit&tenMH=<%=tenMH %>&index=<%=indexPage %>&MaMH=${objMonHoc.maMH}'>Edit</a></td>
											<td><a href='danhsachmonhoc.jsp?stt=view&tenMH=<%=tenMH %>&index=<%=indexPage %>&MaMH=${objMonHoc.maMH}'>View</a></td>
									</tr>
								</c:otherwise>
						</c:choose>
						<%c++; %>
					</c:forEach>
					<input type="hidden" name="xoaMH" id="xoaMH"/>
					
			<script language="JavaScript" type="text/javascript">
				function submit()
				{
					document.getElementById("frmMonHoc").submit();
				}
			
				function checkAll()
				{		
					if (document.getElementById("chkAll").checked == true) {
						<%
							for(int i = 1; i< c; i++) {
								out.print("document.getElementById('chk" + i + "').checked = true;\n");
							}
						%>	
					}else {
						<%
							for(int i = 1; i< c; i++) {
								out.print("document.getElementById('chk" + i + "').checked = false;\n");
							}
						%>
					}
				}
				
				function checkRow(){
					if(document.getElementById('chkAll').checked == true)
						document.getElementById('chkAll').checked = false;
				}
				
				function deleteRow()
				{
					var check = false;
					<%
						for(int i=1;i<c;i++)
						{
							out.print("if(document.getElementById('chk" + i + "').checked == true)");
							out.print("check = true;");
						}
					%>
					if(check == true)
					{
						if(confirm("Bạn có chắc muốn xoá không???"))
						{
							document.getElementById('xoaMH').value = 'xoa';
							document.forms["frmMonHoc"].submit();
						}
					}
					else
						alert("Bạn chưa chọn checkbox nào!!!")
				}
				
				function rightSpace(str)
				{
					var length=str.length-1;
					while(length>=0&&str.charAt(length)==' ')
					{
						length--;
					}
					return str.substr(0,length+1);
				}
				
				function validateNumber(str)
				{
					validate="0123456789";
					for(i=0;i<str.length;i++)
					{
						for(j=0;j<validate.length;j++)
							if(str.charAt(i)==validate.charAt(j))
								j=validate.length;
						if(j!=validate.length+1)
							return false;
					}
					return true;
				}
				
				function validateForm_Insert()
				{
					var maMonHoc=document.getElementById("txtMaMonHoc").value;
					var tenMonHoc=document.getElementById("txtTenMonHoc").value;
					var chungChi=document.getElementById("txtChungChi").value;
					var lePhi=document.getElementById("txtLePhi").value;			
					var stt=true;
					
					if(rightSpace(maMonHoc)!="")
						document.getElementById("alertMaMonHoc").innerHTML="";
					else{
						document.getElementById("alertMaMonHoc").innerHTML="*";
						stt=false;
					}
					
					if(rightSpace(tenMonHoc)!="")
						document.getElementById("alertTenMonHoc").innerHTML="";
					else{
						document.getElementById("alertTenMonHoc").innerHTML="*";
						stt=false;
					}
					
					/*if(rightSpace(thoiGianKetThuc)!="")
						document.getElementById("alertThoiGianKetThuc").innerHTML="";
					else{
						document.getElementById("alertThoiGianKetThuc").innerHTML="*";
						stt=false;
					}*/
					
					if(rightSpace(chungChi)!=""){
						document.getElementById("alertChungChi").innerHTML="";			
					}
					else{
						document.getElementById("alertChungChi").innerHTML="*";
						stt=false;
					}
					
					if(rightSpace(lePhi)!=""){
						document.getElementById("alertLePhi").innerHTML="";			
					}
					else{
						document.getElementById("alertLePhi").innerHTML="*";
						stt=false;
					}
					
					if(stt==false)
						document.getElementById("alertValidate").innerHTML="Bạn phải nhập vào nơi có dấu *";
					else{
						document.getElementById("alertValidate").innerHTML="";
					
						if(!validateNumber(chungChi)){
							document.getElementById("alertChungChi").innerHTML="*";
							stt=false;
						}
						else if(!validateNumber(lePhi)){
							document.getElementById("alertLePhi").innerHTML="*";
							stt=false;
						}
						else
							document.getElementById("alertLePhi").innerHTML="";
					}	
					return stt;		
				}
				
				function click_btnTimTenMH()
				{
					location.href="danhsachmonhoc.jsp?tenMH="+document.getElementById("txtTimTenMonHoc").value;		
				}
		
				function validateForm()
				{
					var tenMonHoc=document.getElementById("txtTenMonHoc").value;
					var thoiGianBatDau=document.getElementById("txtThoiGianBatDau").value;
					var thoiGianKetThuc=document.getElementById("txtThoiGianKetThuc").value;
					var chungChi=document.getElementById("txtChungChi").value;
					var lePhi=document.getElementById("txtLePhi").value;			
					var stt=true;
					
					if(rightSpace(tenMonHoc)!="")
						document.getElementById("alertTenMonHoc").innerHTML="";
					else{
						document.getElementById("alertTenMonHoc").innerHTML="*";
						stt=false;
					}
					
					if(rightSpace(thoiGianBatDau)!="")
						document.getElementById("alertThoiGianBatDau").innerHTML="";
					else{
						document.getElementById("alertThoiGianBatDau").innerHTML="*";
						stt=false;
					}
					
					if(rightSpace(thoiGianKetThuc)!="")
						document.getElementById("alertThoiGianKetThuc").innerHTML="";
					else{
						document.getElementById("alertThoiGianKetThuc").innerHTML="*";
						stt=false;
					}
					
					if(rightSpace(chungChi)!=""){
						document.getElementById("alertChungChi").innerHTML="";			
					}
					else{
						document.getElementById("alertChungChi").innerHTML="*";
						stt=false;
					}
					
					if(rightSpace(lePhi)!=""){
						document.getElementById("alertLePhi").innerHTML="";			
					}
					else{
						document.getElementById("alertLePhi").innerHTML="*";
						stt=false;
					}
					
					if(stt==false)
						document.getElementById("alertValidate").innerHTML="Bạn phải nhập vào nơi có dấu *";
					else{
						document.getElementById("alertValidate").innerHTML="";
						if(!validateNumber(chungChi)){
							document.getElementById("alertChungChi").innerHTML="*";
							stt=false;
						}
						else if(!validateNumber(lePhi)){
							document.getElementById("alertLePhi").innerHTML="*";
							stt=false;
						}
						else
							document.getElementById("alertLePhi").innerHTML="";
					}	
					return stt;	
				}
				
				function click_btnCapNhat()
				{				
					if(validateForm())
					{
						document.getElementById("actionType").value="capnhatmonhoc";
						document.forms['frmMonHoc'].submit();
					}
				}
		
		
				function click_btnThemMoi()
				{
					if(validateForm_Insert())
					{
						document.getElementById("actionType").value="themmoimonhoc";
						document.forms['frmMonHoc'].submit();
					}	
				}
		
				function click_btnThoat(indexPage)
				{
					location.href="danhsachmonhoc.jsp?tenMH="+document.getElementById("txtTimTenMonHoc").value+"&index="+indexPage;		
				}
				
				function rightSpace(str)
				{
					var length=str.length-1;
					while(length>=0&&str.charAt(length)==' ')
					{
						length--;
					}
					return str.substr(0,length+1);
				}
				
				function validateNumber(str)
				{
					validate="0123456789";
					for(i=0;i<str.length;i++)
					{
						for(j=0;j<validate.length;j++)
							if(str.charAt(i)==validate.charAt(j))
								j=validate.length;
						if(j!=validate.length+1)
							return false;
					}
					return true;
				}
			</script>					
					<tr>
						<td colspan="12" style="text-align:right">
						Trang 
							<c:forEach var="numPage" items="${showNumPage}">
								${numPage} 
							</c:forEach>									
						</td>
					</tr>
				</table>
				<br/>
					
			</div>
			<!-- Lay duong cac properties cua  link  -->
				<% String proLink=""; %>
				<c:if test="${not empty param.tenMH}"><% proLink="&tenSV="+request.getParameter("tenMH"); %> </c:if>
				<c:if test="${not empty param.index}"><% proLink=proLink+"&index="+request.getParameter("index"); %> </c:if>
				
				<input type='hidden' value="<%=proLink%>" name="txtProLink" id="txtProLink"/>
			<!-- End -->
		</form>

	<div class="div_footer"><br/>
				Copyright©2011. All right reserved.Theme design by HungVuong Group.
			</div>
</div>
</body>
</html>